package com.kye.express.business.jd.address.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdAddressListBinding;
import com.kye.model.bean.jd.address.JDAddressBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 01/02/2018
 * @updated : 01/02/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDAddressViewBinder extends ItemViewBinder<JDAddressBean, JDAddressViewBinder.ViewHolder> {

    private OnJDAddressListener onJDAddressListener;

    public JDAddressViewBinder(OnJDAddressListener onJDAddressListener) {
        this.onJDAddressListener = onJDAddressListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_address_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDAddressBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdAddressListBinding binding;
        private JDAddressBean item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.select)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            List<JDAddressBean> jdAddressBeans = (List<JDAddressBean>) getAdapter().getItems();
                            Observable.fromIterable(jdAddressBeans)
                                    .doOnNext(new Consumer<JDAddressBean>() {
                                        @Override
                                        public void accept(JDAddressBean jdAddressBean) throws Exception {
                                            if (jdAddressBean.getId().equals(item.getId())) {
                                                jdAddressBean.setSelected(true);
                                            } else {
                                                jdAddressBean.setSelected(false);
                                            }
                                        }
                                    })
                                    .toList()
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<List<JDAddressBean>>() {
                                        @Override
                                        public void accept(List<JDAddressBean> jdAddressBeans) throws Exception {
                                            getAdapter().notifyDataSetChanged();
                                            if (onJDAddressListener != null) {
                                                onJDAddressListener.onSelectAddress(item);
                                            }
                                        }
                                    });
                        }
                    });
            RxView.clicks(binding.addressEdit)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onJDAddressListener != null) {
                                onJDAddressListener.onEditAddress(item);
                            }
                        }
                    });
            RxView.clicks(binding.addressDelete)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onJDAddressListener != null) {
                                onJDAddressListener.onDeleteAddress(item);
                            }
                        }
                    });
        }

        public void bindData(JDAddressBean item) {
            this.item = item;
            binding.setData(item);
        }
    }

    public interface OnJDAddressListener {
        void onSelectAddress(JDAddressBean jdAddressBean);
        void onEditAddress(JDAddressBean jdAddressBean);
        void onDeleteAddress(JDAddressBean jdAddressBean);
    }
}
